<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/23
 * Time: 10:55
 */

namespace App\Http\Controllers\Home;

use App\Model\Apply;
use App\Model\BankCard;
use App\Model\ExceedShop;
use App\Model\Members;
use App\Model\MemberSign;
use App\Model\MembersLevel;
use App\Model\MicroMall;
use App\Model\Poundage;
use App\Model\RecordBonus;
use App\Model\RecordTransaction;
use App\Model\RecordTransferAccounts;
use App\Model\RecordWithdraw;
use App\Model\ShopMemberCart;
use App\Model\ShopOrder;
use App\Model\StoresCenter;
use App\Model\StoresCenterGoods;
use App\Model\StraightShopp;
use Illuminate\Http\Request;
use App\Model\Address;
use App\Model\ShopGoodsImg;
use App\Model\ShopGoods;
use Illuminate\Support\Facades\DB;
use Session;
class VotingController extends BaseController
{
    /**
     * 直商城复投页面
     */
    public function voting()
    {
        return view('Home.member.voting');
    }

    /**
     * 直商城复投支付
     * @param Request $request
     * @return array
     */
    public function votings(Request $request)
    {
        $data = $request->get('users');
        $addressid = $data['addressid'];
        if(empty($addressid)){
            return ['code' => 0 ,'data' => '请选择收货地址'];
        }
        if($data){
            //会员表
            $UserModel = new Members();
            $user = session('home_user');
            $uid = $user->uid;
            $user = $UserModel->where(['uid' => $user->uid])->first();
            $company_id = $user['company_id'];
            $money = 3000;
            $freight = 18;//运费
            $rico = VOTING($user['credit1'],$user['credit2'], $user['credit6'],$money,$freight);
            //判断金额
            if($rico['code'] == 1){
                DB::beginTransaction(); //开启事务
                //查询微商城等级
                //扣除当前登录的金额
                $userData = [ // DATA
                    'credit1' => $user['credit1'] - $rico['data']['credit1'],
                    'credit2' => $user['credit2'] - $rico['data']['credit2'],
                    'credit6' => $user['credit6'] - $rico['data']['credit6'],
                    'is_credit6' => 0,
                ];
                $userOrder = $UserModel->where(['uid' => $uid])->update($userData);
                if (empty($userOrder)) {
                    DB::rollback();  //回滚
                    return ['code' => 0, 'data' => '参数错误'];
                }
                $ordersn = ORDER_SN();
                $addressDataAdd = Address::where( [ 'id' => $addressid ] )->select( 'name', 'phone', 'province', 'city', 'district', 'address' )->first();
                $order = [
                    'uid'           => $uid,
                    'ordertype'     => 1, //1 直商城 2 微商城 3 0元商城
                    'ordersn'       => $ordersn,
                    'price'         => $money,
                    'originalprice' => $money,
                    'status'        => 1,//-1取消状态，0代付款，1为已付款，2为已发货，3为成功
                    'createtime'    => time(), //下单时间
                    'paytime'       => time(),//支付时间
                    'addressid'     => $addressid,
                    'receipt_name'  => $addressDataAdd['name'],
                    'receipt_adr'   => $addressDataAdd['province'].$addressDataAdd['city'].$addressDataAdd['district'].$addressDataAdd['address'],
                    'receipt_phone' => $addressDataAdd['phone'],
                    'mall_goodsid'  => 0,
                    'type'          => 1,
                    'remark'        => '复投',
                    'mall_num'      => '冷敷贴：'.$data['stick'].'盒 ，百草液：'.$data['liquid'].'瓶',
                    //'mall_num'      => '百草液：'.$data['stick'].'瓶 ，百草液：'.$data['liquid'].'瓶',
                    'updated_at' => date('Y-m-d H:i:s'),
                    'created_at' => date('Y-m-d H:i:s'),
                ];
                //订单表
                $ShopOrderModel = new ShopOrder();
                $ricoOrder = $ShopOrderModel->add_order($order);
                if(empty($ricoOrder)){
                    DB::rollback();  //回滚
                    return ['code' => 0, 'data' => '交易失败'];
                }
                //交易记录表
                $RecordTransaction = new RecordTransaction();
                $recommendData = [
                    'uid'       => $uid,
                    'ordersn'   => $ordersn,//关联order表订单编号
                    'money'     => -$money,
                    'type'      => 1,
                    'time'      => time(),
                    'explain'   => '直商城',
                    'company_id' => $company_id,
                    'remark'   => '电子币：'.-$rico['data']['credit1'].'，奖励币：'.-$rico['data']['credit2'].'，复投币：'.-$rico['data']['credit6'],
                ];
                $TransactionId = $RecordTransaction->add_transaction($recommendData);
                if(empty($TransactionId)){
                    DB::rollback();  //回滚
                    return ['code' => 0, 'data' => '参数错误'];
                }
                DB::commit();  //提交
                return ['code' => 1, 'data' => '支付成功'];
            } else {
                return ['code' => 0, 'data' => $rico['data']];
            }
        }
    }
}